package com.wagn.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wagn.config.exceptionHandler.MyException;
import com.wagn.eduservice.entity.EduCourse;
import com.wagn.eduservice.entity.EduCourseDescription;
import com.wagn.eduservice.entity.EduVideo;
import com.wagn.eduservice.entity.course.QueryCourse;
import com.wagn.eduservice.entity.vo.VoCourseInfo;
import com.wagn.eduservice.entity.vo.VoSetp3;
import com.wagn.eduservice.mapper.EduCourseMapper;
import com.wagn.eduservice.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-08-17
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;
    @Autowired
    private EduVideoService eduVideoService;
    @Autowired
    private EduChapterService eduChapterService;
    //
    @Override
    public String saveCourseInfo(VoCourseInfo voCourseInfo) {

        //存course表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(voCourseInfo,eduCourse);
        if(this.baseMapper.insert(eduCourse)==0){
            throw new MyException(20001,"保存失败");
        }else{
            //存course描述表
            EduCourseDescription eduCourseDescription = new EduCourseDescription();

            eduCourseDescription.setId(eduCourse.getId());
            eduCourseDescription.setDescription(voCourseInfo.getDescription());
            if(!eduCourseDescriptionService.save(eduCourseDescription)){
                throw new MyException(20001,"保存失败");
            }else{
                return eduCourseDescription.getId();
            }
        }
    }

    @Override
    public VoCourseInfo getInfoById(String id) {
        VoCourseInfo rs = new VoCourseInfo();
        EduCourse info1 = this.getById(id);
        EduCourseDescription info2 = eduCourseDescriptionService.getById(id);
        BeanUtils.copyProperties(info1,rs);
        rs.setDescription(info2.getDescription());
        System.out.println("------------------------------------------------------"+rs.toString());
        return rs;
    }

    @Override
    public String updateCourseInfo(VoCourseInfo voCourseInfo) {
        //更新course表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(voCourseInfo,eduCourse);
        if(!this.updateById(eduCourse)){
            throw new MyException(20001,"更新失败");
        }else{
            //更新course描述表
            EduCourseDescription eduCourseDescription = new EduCourseDescription();
            eduCourseDescription.setId(eduCourse.getId());
            eduCourseDescription.setDescription(voCourseInfo.getDescription());
            if(!eduCourseDescriptionService.updateById(eduCourseDescription)){
                throw new MyException(20001,"更新失败");
            }else{
                return eduCourseDescription.getId();
            }
        }
    }

    @Override
    public VoSetp3 getVoStep3(String courseId) {
        VoSetp3 voStep3 = baseMapper.getVoStep3(courseId);
        return voStep3;
    }

    @Override
    public Page<EduCourse> pageConditionCourse(QueryCourse queryCourse) {
        //初始化page
        Page<EduCourse> page = new Page<>(queryCourse.getCurrent(), queryCourse.getLimit());

        QueryWrapper wrapper = new QueryWrapper<EduCourse>();

        if(!StringUtils.isEmpty(queryCourse.getTitle()) ){
            wrapper.like("title",queryCourse.getTitle());
        }

        if(!StringUtils.isEmpty(queryCourse.getStatus()) ){
            wrapper.eq("status",queryCourse.getStatus());
        }

        wrapper.orderByDesc("status");
        wrapper.orderByDesc("gmt_create");

        this.page(page, wrapper);
        return page;
    }

    @Override
    public boolean delete(String id) {
        QueryWrapper wrapper = new QueryWrapper();

        wrapper.eq("course_id",id);
        eduVideoService.remove(wrapper);
        eduChapterService.remove(wrapper);
        eduCourseDescriptionService.removeById(id);
        this.removeById(id);

        return true;
    }
}
